package Demo10;

/**
 * 无重复字符的最长子串
 * https://leetcode.cn/problems/longest-substring-without-repeating-characters/
 * 使用滑动数组思想,
 * 创建两个指针,一个左指针一个右指针,创建一个长度位128的数组来模拟实现哈希表
 * 然后就是窗口,然后判断,出窗口,一直循环到right 不小于0 然后返回最后的值
 */
class Solution {
    public int lengthOfLongestSubstring(String s) {
        int[] hash = new int[128];
        char[] ch = s.toCharArray();
        int left = 0,right = 0,len = hash.length,ret = 0;
        while(right < ch.length) {
            hash[ch[right]]++;
            while(hash[ch[right]] > 1) {
                hash[ch[left++]]--;
            }
            ret = Math.max(ret,right - left+1);
            right++;
        }
        return ret;
    }
}
class Demo {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int ret = solution.lengthOfLongestSubstring("abcabcbb");
        System.out.println(ret);
    }
}